package pierwszy;
 


import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class ksPortalLogowanie {
 
    static WebDriver driver;
    static WebDriverWait wait;
 
    @BeforeClass
    public void setUp() {
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver,30);
        driver.manage().window().maximize();
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
 
    @Test (dataProvider = "getCorrectLoginCredentials")
    public void logInWithCorrectCredentials(String login, String password)
            throws InterruptedException {
    			System.out.println("logInWithIncorrectCredentials");
    			openLogInPage();   			
    			logIn(login, password);
    			//checkIfLogged();
    }
    
    @DataProvider
    public String[][] getCorrectLoginCredentials(){
    	return new String[][]{{"p.jaworek","jawso98*"}};
    }
    
    @DataProvider
    public String[][] getSites(){
    	return new String[][]{{"ks_portal"},{"ks_dotacje"},{"ks_ksiegowosc"}};
    }
    
	private void logIn(String login, String password) {
        WebElement lgn = driver.findElement(UiMapping.LoginField);
        WebElement pswd = driver.findElement(UiMapping.PasswordField);
        WebElement lgnbtn = driver.findElement(UiMapping.LoginButton);
        
        lgn.sendKeys(login);
        pswd.sendKeys(password);
        lgnbtn.click();
	}

	private void openLogInPage() {
		driver.get("http://www.bermar2.home.pl/ks_portal/login.php");
	}
	
	
	@Test (/*dependsOnMethods="logInWithCorrectCredentials",*/ dataProvider = "getSites")
	private void checkIfLogged(String sites) throws InterruptedException {
		System.out.println("checkIfLogged");
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.LogOutButton));

		WebElement ksSuite =driver.findElement(UiMapping.passSite(sites));
		wait.until(ExpectedConditions.elementToBeClickable(ksSuite));
		ksSuite.click();
		wait.until(ExpectedConditions.elementToBeSelected(ksSuite));
		assert ksSuite.isSelected();		
	}
	
	
	private void chooseSite(String site){
		WebElement ksPortal = driver.findElement(UiMapping.passSite(site));	
		ksPortal.click();
		
		WebElement wybieramBtn = driver.findElement(UiMapping.ChooseBtn);
		wybieramBtn.click();
		assert driver.findElement(UiMapping.SelectedSite).getText().equals(site);
	}
	
	private void getOptionFromIconPlus(String optionIconPlus){
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.IconPlus));
		WebElement ikonaPlus = driver.findElement(UiMapping.IconPlus);
		ikonaPlus.click();
		
		WebElement note = driver.findElement(UiMapping.passOptionOfIconPlus(optionIconPlus));
		wait.until(ExpectedConditions.elementToBeClickable(note));
		note.click();
	}
	
	@Test //(dependsOnMethods="checkIfLogged")
	private void addNote() throws InterruptedException{
		System.out.println("addNote");
		String noteContent = "Notka1 WP.";
		
		chooseSite("ks_portal");
		
		getOptionFromIconPlus(" Notkę  ");
		
		Thread.sleep(3000);
		WebElement textArea = driver.findElement(UiMapping.NoteTextArea);
//		wait.until(ExpectedConditions.elementToBeSelected(textArea));
		textArea.sendKeys(noteContent);
		
		WebElement zapisBtn = driver.findElement(UiMapping.NoteSaveButton);
		zapisBtn.click();
		
		Thread.sleep(3000);
		assert driver.findElement(UiMapping.passNoteContentCheck(noteContent)).getText().equals(noteContent);
		
		Thread.sleep(2000);
		WebElement usunBtn = driver.findElement(UiMapping.passNoteContentButton(noteContent));
		usunBtn.click();
		
		Thread.sleep(2000);
		driver.switchTo().alert().accept();
	}
	
	@Test //(dependsOnMethods="addNote")
	private void addAktualnosc() throws InterruptedException{
		System.out.println("addAktualnosc");
		String title = "Ttile1WP";
		String shortage = "Shortage1WP";
		String text = "Text1\nWP\nTest";
		
		chooseSite("ks_portal");
		
		getOptionFromIconPlus("Aktualność ");
		
		//setTitle
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsTitle));
		WebElement titleTextArea = driver.findElement(UiMapping.NewsTitle);
		titleTextArea.sendKeys(title);
		
		
		//setShortage
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsShortage));
		WebElement shortageTextArea = driver.findElement(UiMapping.NewsShortage);
		shortageTextArea.sendKeys(shortage);

		//setText		
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsTextArea));
		WebElement textTextArea = driver.findElement(UiMapping.NewsTextArea);
		textTextArea.sendKeys(text); 
		
		//submit
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsSubmitButton));
		WebElement submitBtn = driver.findElement(UiMapping.NewsSubmitButton);
		submitBtn.click();
		
		//check 
////		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passNewsTitle(title)));
////		WebElement news = driver.findElement(UiMapping.passNewsTitle(title));
//		assert news.getText().equals(title);
//		news.click();
		
		//checkDetails
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.NewsTextTab));
		WebElement newsTextTab = driver.findElement(UiMapping.NewsTextTab);
		newsTextTab.click();

		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.NewsTextAreaCheck));
		WebElement newsText = driver.findElement(UiMapping.NewsTextAreaCheck);
		System.out.println(newsText.getText());
		assert newsText.getText().equals(text);
		
		//removeNews
		wait.until(ExpectedConditions.elementToBeClickable(UiMapping.NewsRemoveButton));
		WebElement newsRemoveBtn = driver.findElement(UiMapping.NewsRemoveButton);
		newsRemoveBtn.click();
		driver.switchTo().alert().accept();
	
		
	}
 
    @AfterClass
    public static void tearDown() {
    	driver.quit();
        //driver.close();
    }
 
}
